<?php
class HotelController extends CBaseController
{
	protected $localConfig;
	protected $hotel;
	protected $validator;
	
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		if(is_file(dirname(__FILE__).'/HotelController.cfg.php'))
		$this->localConfig=include(dirname(__FILE__).'/HotelController.cfg.php');
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
		$this->templator->assign('menu','general');
	}
	
	public function all()
	{
	 $hotels = R::findAll('hotel',' ORDER BY name');
	 $this->templator->assign('hotels',array_merge($hotels, array()));
	 $this->templator->display('hotel_list.html');
	}
	
	public function savePrepare()
	{
		$this->hotel = R::dispense('hotel');
		$this->hotel->name = $_POST['name'];
		$this->hotel->hotelcode = $_POST['hotelcode'];
		$this->hotel->shortdesc =  $_POST['shortdesc'];
		$this->hotel->longdesc = $_POST['longdesc'];
		$this->hotel->type = $_POST['type'];
	}
	
	public function updatePrepare()
	{
		$id = $_POST['id'];
		$this->hotel = R::load('hotel',$id);
		$this->hotel->name = $_POST['name'];
		$this->hotel->hotelcode = $_POST['hotelcode'];
		$this->hotel->shortdesc =  $_POST['shortdesc'];
		$this->hotel->longdesc = $_POST['longdesc'];
		$this->hotel->type = $_POST['type'];
	}
	
	public function saveORupdateValidate()
	{
		$this->validator = new FormValidator();
		$this->validator->addValidation("name","req","Please fill in Name");
		return $this->validator->ValidateForm();
	}
		
	
	public function create()
	{
		$this->hotel = R::dispense('hotel');
		$this->templator->assignByRef('hotel',$this->hotel);
		$this->templator->display('hotel_general.html');
	}
	public function save()
	{
	  $this->savePrepare();
	  if($this->saveORupdateValidate())
	  {
		R::store($this->hotel);
		$this->templator->assign('info','Hotel added sucessfully');
		$this->templator->assignByRef('hotel',$this->hotel);		
		$this->templator->display('hotel_general.html');	
	  }
	  else
	  {
		$error_hash = $this->validator->GetErrors();
		$this->templator->assign('validate_errors',$error_hash);
		$this->templator->assignByRef('hotel',$this->hotel);
		$this->templator->display('hotel_general.html');
	  }
	}
	
	public function edit()
	{
		$id = $_GET['id'];
		if(isset($id))
		$this->hotel = R::load('hotel',$id);
		else
		$this->hotel = R::dispense('hotel');
		$this->templator->assignByRef('hotel',$this->hotel);		
		$this->templator->display('hotel_general.html');
	}
	
	public function update()
	{
		$this->updatePrepare();
		  if($this->saveORupdateValidate())
		  {
			R::store($this->hotel);
			$this->templator->assignByRef('hotel',$this->hotel);
			$this->templator->assign('info','Hotel updated sucessfully');
			$this->templator->display('hotel_general.html');	
		  }
		  else
		  {
			$error_hash = $this->validator->GetErrors();
			$this->templator->assign('validate_errors',$error_hash);
			$this->templator->assignByRef('hotel',$this->hotel);
			$this->templator->display('hotel_general.html');
		  }
	}
	
	public function delete()
	{
	 $id = $_GET['id'];
	 if(isset($id))
	 {
		$hotel = R::load('hotel',$id);
		if(isset($hotel))
		{
			R::trash($hotel);
			$this->templator->assign('info','Hotel deleted sucessfully');
		}
	 }
	 $this->all();
	}
}	